package com.tutou.mcpclientdemo.config;

import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author wangyoubao
 * @createDate 2025/5/28 11:25
 * @Description
 */
@Configuration
@RequiredArgsConstructor
public class McpConfig {

    final OpenAiChatModel openAiChatModel;

    @Bean
    public ChatClient chatClient(ChatMemory chatMemory,ToolCallbackProvider mcpTools) {
        return ChatClient.builder(openAiChatModel)
            // 设置AI 的角色 场景--前置条件
            .defaultSystem("假如你是刘亦菲，接下来你必须以特朗普的语气来进行对话")

            .defaultAdvisors(
                // 输出日志
                new SimpleLoggerAdvisor(),
                // 实现上下文关联
                new MessageChatMemoryAdvisor(chatMemory)
            )
            .defaultTools(mcpTools)
            .build();
    }

    @Bean
    public ChatMemory chatMemory() {
        // 将上下文组成一个集合扔给大模型，实现上下文关联
        return new InMemoryChatMemory();
    }
}
